package com.company.bookstore.controller.data_module;

import com.company.bookstore.common.result.MvcResult;
import com.company.bookstore.entities.book_module.Category;
import com.company.bookstore.mapper.dataMapper.DataMapper;
import com.company.bookstore.service.book_module.CategoryService;
import com.company.bookstore.service.data_module.DataService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "系统：数据分析")
@Slf4j
@RestController
@RequestMapping("/api/data")
public class DataController {

    @Resource
    CategoryService categoryService;

    @Resource
    DataService dataService;

    @Resource
    DataMapper dataMapper;

    @PostMapping("/testTime")
    public ResponseEntity<Object> getTestTime(@RequestBody Map<String, String> time ){
        System.out.println(time.get("start"));
        System.out.println(time.get("end"));
        return new ResponseEntity<>(dataMapper.getClickToPayRate(time.get("start"), time.get("end")), HttpStatus.OK);

    }@PostMapping("/getAllGoodsData")
    public ResponseEntity<Object> getAllGoodsData(@RequestBody Map<String, String> time){
        System.out.println(time.get("start"));
        System.out.println(time.get("end"));
        return new ResponseEntity<>(dataService.getAllData(time.get("start"), time.get("end")), HttpStatus.OK);
    }

    @PostMapping("/getAllOrderData")
    public ResponseEntity<Object> getAllOrderData(@RequestBody Map<String, String> time){
        return new ResponseEntity<>(dataService.getAllOrderData(time.get("start"), time.get("end")), HttpStatus.OK);
    }

    @PostMapping("/getDashBoardData")
    public ResponseEntity<Object> getDashBoardData(){
        return new ResponseEntity<>(dataService.getDashBoardData(), HttpStatus.OK);
    }

    @PostMapping("/getSum")
    public MvcResult<Object> getSum(){
        // 获取图书标签
        List<Category> categoryList = categoryService.list();
        HashMap<String, Integer> map = new HashMap<>();
        for(Category category:categoryList){
            map.put(category.getName(),dataService.getSum(category.getName()));
        }
        MvcResult.SELECT_SUCCESS.setObject(map);
        return MvcResult.SELECT_SUCCESS;
    }
}
